﻿#include "BrowserTableHandlerMaterialIsoSolid.h"
#include "OperatorsInterface/OperAbstractMaterial.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsPhysics.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsAbsMaterial.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsMaterialsIsosolid.h"
#include "FITK_Kernel/FITKCore/FITKOperatorRepo.h"

namespace GUI
{
    void GUI::BrowserTableHandlerMaterialIsoSolid::updateTable()
    {
        if (_tableWidget == nullptr) return;
        _tableWidget->clear();
        _tableWidget->setColumnCount(0);
        _tableWidget->setRowCount(0);

    //设置表头
        QStringList headerLabels;
        headerLabels  << tr("Name") << tr("Density [kg/m3]") << tr("Tensile Modulus [Pa]") <<
            tr("Shear Modulus [Pa]")  << tr("Poisson Ratio") << tr("Notes");
        _tableWidget->setColumnCount(headerLabels.size());
        _tableWidget->setHorizontalHeaderLabels(headerLabels);
    //更新表格
        this->updateData();
    }

    void GUI::BrowserTableHandlerMaterialIsoSolid::updateData()
    {
        if (_tableWidget == nullptr) return;
        Acoustics::FITKAcousticsPhysics* phys = Acoustics::FITKAcousticsPhysics::GetAcousticsPhysicsFromFrame();
        if (phys == nullptr) return;
    //获取材料管理器
        Acoustics::FITKAcousticsMaterialManager* matmgr = phys->getMaterialManager();
        if (matmgr == nullptr) return;

        QList<Acoustics::FITKAcousticsAbsMaterial*> matList = matmgr->getAcousticsMaterials(
            Acoustics::FITKAcousticsAbsMaterial::ISOSolid);
    //材料数量
        const int n = matList.size();
        _tableWidget->setRowCount(n);

        for (int i = 0; i < n; ++i)
        {
        //转换为各向同性固体材料
            Acoustics::FITKAcousticsMaterialsIsosolid* maI = dynamic_cast<Acoustics::FITKAcousticsMaterialsIsosolid*>
                (matList.at(i));
            if (maI == nullptr) continue;
        //数据ID
            const int id = maI->getDataObjectID();
        //填充表格，用type表示ID
            _tableWidget->setItem(i, 0, new QTableWidgetItem(maI->getDataObjectName(), id));
            _tableWidget->setItem(i, 1, new QTableWidgetItem(QString::number(maI->getDensity()), id));
            _tableWidget->setItem(i, 2, new QTableWidgetItem(QString::number(maI->getTensileModulus()), id));
            _tableWidget->setItem(i, 3, new QTableWidgetItem(QString::number(maI->getShearModulus()), id));
            _tableWidget->setItem(i, 4, new QTableWidgetItem(QString::number(maI->getPoissonRatio()), id));
            _tableWidget->setItem(i, 5, new QTableWidgetItem(maI->getMaterialDescribe(), id));
        }
        _tableWidget->autoResizeCellWidth();
        _tableWidget->setTableItemEditable(false);

    }

    EventOper::OperAbstractMaterial * BrowserTableHandlerMaterialIsoSolid::getMaterialOper()
    {
    //从操作器仓库获取
        return Core::FITKOperatorRepo::getInstance()->
            getOperatorT<EventOper::OperAbstractMaterial>("OperMaterialIsoSolid");
    }

    bool BrowserTableHandlerMaterialIsoSolid::enableAttriSet()
    {
        QList<QTableWidgetItem*> selectItems = this->getSelectItems();
        if (selectItems.size() < 2) return false;

        QTableWidgetItem* item = selectItems.at(0);
        if (item == nullptr) return false;
        //名称列
        const int colIndex = item->column();
        //只有密度能批量
        if (colIndex != 1) return false;

        return true;
    }
}